package com.lmx.persistence.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.common.BaseCrudServiceImpl;
import com.lmx.repoDomain.tmodel.FoodPlanDayRecord;
import com.lmx.repoDomain.tmodel.unpersis.QueryParam;

/**
 * 老人用餐统计(日)Service实现
 *
 * @author ytche
 * @date 2017年9月1日
 */
@Service
public class FoodPlanDayRecordServiceImpl extends
		BaseCrudServiceImpl<FoodPlanDayRecord> implements
		FoodPlanDayRecordService {

	@Override
	@Transactional(readOnly = true)
	public List<FoodPlanDayRecord> findDayRecord(Long tenantId,
			QueryParam pageParam) {
		Integer startTime = CommonUtil.getDayBegin(new Integer(pageParam
				.getFilter().get("diningDate").toString()));
		Integer endTime = startTime + 24 * 3600;
		StringBuilder sqlSb = new StringBuilder(
				"select count(*) as num,record.* from tnb_food_day_plan_record_view as record ")
				.append("where record.dining_date>= :startTime and record.dining_date < :endTime")
				.append(" and record.tenant_id=:tenantId ");
		if (pageParam.getFilter().containsKey("diningTime.id")) {
			sqlSb.append(" and dining_time_id =:diningTimeId ");
		}
		if (pageParam.getFilter().containsKey("floorId")) {
			sqlSb.append(" and floor_id =:floorId");
		}
		if (pageParam.getFilter().containsKey("buildingId")) {
			sqlSb.append(" and building_id =:buildingId");
		}
		sqlSb.append(" GROUP BY record.food_id ");
		StringBuilder countSqlSb = new StringBuilder();
		countSqlSb.append("select count(*) from (").append(sqlSb)
				.append(") as c");
		Session session = this.em.unwrap(Session.class);
		Query dataQuery = session.createSQLQuery(sqlSb.toString())
				.addEntity(FoodPlanDayRecord.class)
				.setInteger("startTime", startTime)
				.setInteger("endTime", endTime).setLong("tenantId", tenantId);
		Query countQuery = session.createSQLQuery(countSqlSb.toString())
				.setInteger("startTime", startTime)
				.setInteger("endTime", endTime).setLong("tenantId", tenantId);
		if (pageParam.getFilter().containsKey("diningTime.id")) {
			dataQuery.setLong("diningTimeId", new Long(pageParam.getFilter()
					.get("diningTime.id").toString()));
			countQuery.setLong("diningTimeId", new Long(pageParam.getFilter()
					.get("diningTime.id").toString()));
		}
		if (pageParam.getFilter().containsKey("floorId")) {
			dataQuery.setLong("floorId",
					new Long(pageParam.getFilter().get("floorId").toString()));
			countQuery.setLong("floorId",
					new Long(pageParam.getFilter().get("floorId").toString()));
		}
		if (pageParam.getFilter().containsKey("buildingId")) {
			dataQuery
					.setLong("buildingId",
							new Long(pageParam.getFilter().get("buildingId")
									.toString()));
			countQuery.setLong("buildingId", new Long(pageParam.getFilter()
					.get("buildingId").toString()));
		}
		Page<FoodPlanDayRecord> page = super.pageByQuery(dataQuery, countQuery,
				pageParam);
		return page.getContent();
	}
}
